package com.garrett.demo.sys.service;

import com.garrett.demo.sys.dao.UserDataBaseDemo;
import com.garrett.demo.sys.entity.Account;
import com.garrett.demo.sys.entity.SimsStudent;
import org.springframework.stereotype.Service;

import java.util.Arrays;

@Service
public class AccountServiceImp extends AbstractAccountService{
    private UserDataBaseDemo user=new UserDataBaseDemo();
    /**
     *
     * @param pw1 密码1
     * @param pw2 密码2
     * @return true-密码一致 false-密码不匹配
     */
    @Override
    public boolean verifyPassword(String pw1,String pw2) {
        boolean flag = false;

        //检测密码长度是否在6-16位
        if (pw1.length()<6 || pw1.length()>16){

            System.out.println("密码长度要在6到16位");
            System.out.println("请重新输入");
            flag = true;

            return flag;
        }

        //检测密码是否相等
        if(!pw1.equals(pw2)){

            System.out.println("两次输入的密码不一样");
            System.out.println("请重新输入");
            flag = true;
        }

        return flag;
    }

    @Override
    public boolean verifyIdCard(String idCard){

        boolean flag = false;
        //检测身份证号长度是否正确
        if (idCard.length()!=18) {

            System.out.println("身份证号错误，身份证号是18位的");
            System.out.println("请重新输入");
            flag = true;

        }
        //检测身份证号的格式是否正确
        else{
            for (int i = 0;i < idCard.length()-1;i++){
                if(idCard.charAt(i) < '0'|| idCard.charAt(i) > '9'){

                    System.out.println("身份证号错误，身份证号格式错误");
                    System.out.println("请重新输入");
                    flag = true;

                    return flag;
                }
            }

            if (idCard.charAt(idCard.length()-1) < '0'|| idCard.charAt(idCard.length()-1) > '9'|| idCard.charAt(idCard.length()-1) == 'X'){
                System.out.println("身份证号错误，身份证号格式错误");
                System.out.println("请重新输入");

            }
        }

        return flag;
    }

    @Override
    public boolean verifyStuId(String id) {

        SimsStudent[] students = user.getStudents();
        boolean flag = false;

        //检测学号长度是否正确
        if (id.length()!=9) {
            System.out.println("学号错误，学号是9位数字");
            System.out.println("请重新输入");
            flag =true;
        }

        //检测学号格式是否正确
        else {
            for (int i = 0;i < id.length();i++){
                if(id.charAt(i)<'0'||id.charAt(i)>'9') {

                    System.out.println("学号错误，学号是9位数字");
                    System.out.println("请重新输入");
                    flag=true;

                }
            }
        }

        if(flag){
            return flag;
        }
        //册检测学号是否有重复注
        else {
            if (students[0] == null)
                return false;
            for (int i = 0; i < 3; i++) {
                if(students[i]!=null) {
                    if (students[i].getStudentId().equals(id)) {

                        System.out.println("学号已被注册");
                        flag = true;

                    }
                }
            }
            if (students[2] != null){
                System.out.println("注册用户已满无法注册");
                flag = true;
            }
        }
        return flag;
    }

    public boolean verifymobilePhone(String mobilePhone) {
        boolean flag = false;
        //检测手机号格式
        for (int i=0;i<mobilePhone.length();i++){
            if(mobilePhone.charAt(i)<'0'||mobilePhone.charAt(i)>'9'){

                System.out.println("手机号为11数字或13位数字（中国地区+86）");
                System.out.println("请重新输入");

                flag = true;
                return flag;
            }
        }

        if(mobilePhone.length() != 11){
            if(mobilePhone.charAt(0) == '8' && mobilePhone.charAt(1) == '6' && mobilePhone.length()==13){

                char c1=mobilePhone.charAt(2);
                char c2=mobilePhone.charAt(3);

                if (c1 == '1' && (c2=='3'||c2=='5'||c2=='7'||c2=='8')){
                    return false;
                }
                else {
                    System.out.println("手机号不符合中国手机号格式");
                    System.out.println("请重新输入");
                    return true;
                }
            }
            else{
                System.out.println("手机号不符合中国手机号格式");
                System.out.println("请重新输入");
                return true;
            }
        }
        else{
            char c1 = mobilePhone.charAt(0);
            char c2 = mobilePhone.charAt(1);
            if (c1=='1'&&(c2=='3'||c2=='5'||c2=='7'||c2=='8')){
                return false;
            }
            else{
                System.out.println("手机号不符合中国手机号格式");
                System.out.println("请重新输入");
                return true;
            }
        }
    }
    public boolean verifygender(String gender,String idCardNo){
        //检测性别格式和核对身份证号上的性别信息
        if (((idCardNo.charAt(16)-'0')%2==1&&gender.equals("男")) || ((idCardNo.charAt(16)-'0')%2==0&&gender.equals("女"))){
            return false;
        }
        System.out.println("性别错误");
        System.out.println("请重新输入");
        return true;
    }
    @Override
    public SimsStudent checkLogin(String account, String password) {

        SimsStudent students = user.getStudentById(account);
        Account accounts = user.getUserByAccount(account);
        //检验密码
        if (accounts.getPassword().equals(password)) {
            SimsStudent a = students;
            return a;
        }

        return null;
    }

    public String toString(String account,String password){
        SimsStudent[] students = user.getStudents();
        SimsStudent a = checkLogin(account,password);

        return a.toString();
    }



    @Override
    public void createAccount(Object... obj) {
        user.addAccount((Account) obj[0]);
        user.createUser((SimsStudent) obj[1]);
    }

    //New modify 12.6

    @Override
    public void deleteAccount(String account) {     //new update 12.10
        //结果展示
        if(user.delAccountById(account)==1&&user.delStudentInfo(account)==1){
            System.out.println("删除成功！");
        }
        else {
            System.out.println("该账号不存在，删除失败！");
        }


    }

    public Account getUserByAccount(String account){
        return user.getUserByAccount(account);
    }

    @Override
    public Account[] getAllAcounts() {  //new update 12.10
        return user.getAllAccount();
    }

    @Override
    public SimsStudent[] getAllStudents() { //new update 12.10
        return user.getAllStudent();
    }

    @Override
    public SimsStudent getStudentById(String stuId) {
        return user.getStudentById(stuId);
    }

    @Override
    public void modify(Object... obj) {
        user.modifyAccount((Account) obj[0]);
        user.modifyStudentInfo((SimsStudent) obj[1]);
    }
}